package com.crawler.service;


import com.crawler.mapper.PageUrlMapper;
import com.crawler.pojo.PageData;
import com.crawler.utils.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/data")
public class TestService {

    private static final Logger LOG = LoggerFactory.getLogger(TestService.class);

    @Autowired
    private PageUrlMapper pageUrlMapper;

    @RequestMapping(value = "/add", method = {RequestMethod.GET})
    public Response getData(final @RequestParam(value = "pageUrl", required = false) String pageUrl,
                            final @RequestParam(value = "title", required = false) String title,
                            final @RequestParam(value = "photoSrc", required = false) String photoSrc,
                            final @RequestParam(value = "nextUrl", required = false) String nextUrl){
        Response response = new Response();
        response.setData("ok");
        response.setSuccess(true);
        PageData insertData = getPageUrl(pageUrl, title, photoSrc, nextUrl);
        pageUrlMapper.insertPageUrl(insertData);
        LOG.info("插入新数据：{}", insertData);

        return response;
    }


    private PageData getPageUrl(String pageUrl, String title, String photoSrc, String nextUrl ) {
        return new PageData(pageUrl, title, photoSrc, nextUrl, new Date());
    }



}
